package com.hui.servlet;

import com.hui.entity.Category;
import com.hui.service.CategoryService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description 分类(Category)控制层
 * @date 2023/2/10 19:53
 */
@WebServlet(urlPatterns = {"/getCategoryAll", "/delCategory", "/toUpdateCategory", "/updateCategory", "/addCategory"})
public class CategoryServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        if (uri.endsWith("getCategoryAll")) {
            getCategoryAll(req, resp);
        } else if (uri.endsWith("delCategory")) {
            delCategory(req, resp);
        } else if (uri.endsWith("toUpdateCategory")) {
            toUpdateCategory(req, resp);
        } else if (uri.endsWith("updateCategory")) {
            updateCategory(req, resp);
        } else if (uri.endsWith("addCategory")) {
            addCategory(req, resp);
        }

    }

    /**
     * 添加分类数据
     **/
    private void addCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端参数
        String categoryName = req.getParameter("categoryName");
        //将获取的参数赋值给category对象
        Category category = new Category(null, categoryName, "1");

        //调用CategoryService中的新增方法
        CategoryService service = new CategoryService();
        service.insertCategory(category);

        //重定向到getCategoryAll--分类列表
        resp.sendRedirect("getCategoryAll");
    }

    /**
     * 修改分类数据
     **/
    private void updateCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端参数
        int id = Integer.parseInt(req.getParameter("categoryId"));
        String categoryName = req.getParameter("categoryName");
        //将获取的参数赋值给category对象
        Category category = new Category(id, categoryName, "1");

        //调用CategoryService中的修改数据方法
        CategoryService service = new CategoryService();
        service.updateCategoryById(category);

        //重定向到getCategoryAll--分类列表
        resp.sendRedirect("getCategoryAll");

    }

    /**
     * 根据id查询数据，用于点击修改时跳转到修改界面回显数据
     **/
    private void toUpdateCategory(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端参数
        int id = Integer.parseInt(req.getParameter("id"));
        //调用CategoryService中的根据id查找数据方法
        CategoryService service = new CategoryService();
        Category category = service.findCategoryById(id);

        //将返回的category数据转发给前端的修改界面
        req.setAttribute("category", category);
        req.getRequestDispatcher("category_update.jsp").forward(req, resp);

    }

    /**
     * 根据id删除分类数据
     **/
    private void delCategory(HttpServletRequest req, HttpServletResponse resp) {
        //获取前端参数
        int id = Integer.parseInt(req.getParameter("id"));
        //调用CategoryService中的删除方法
        CategoryService service = new CategoryService();
        service.delCategoryById(id);
    }

    /**
     * 查询所有的分类数据
     **/
    private void getCategoryAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        CategoryService service = new CategoryService();
        List<Category> list = service.findCategoryAll();
        //将返回的list集合数据转发给前端界面
        req.setAttribute("list", list);
        req.getRequestDispatcher("category_list.jsp").forward(req, resp);
    }
}
